package com.itfallrain.service;

import com.itfallrain.dao.AuthMapper;
import com.itfallrain.dao.RoleAuthMapper;
import com.itfallrain.dao.RoleMapper;
import com.itfallrain.entity.Role;
import com.itfallrain.entity.RoleAuth;
import com.itfallrain.entity.RoleExtend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AuthService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleAuthMapper roleAuthMapper;
    @Autowired
    private AuthMapper authMapper;
    public long getRoleCount(){
        return roleMapper.getRoleCount();
    }
    public List<Map<String,Object>> getRoles(int page,int limit){
        Map<String,Object> map=new HashMap();
        int start=(page-1)*limit;
        map.put("start",start);
        map.put("limit",limit);
        return roleMapper.selectUserAll(map);
    }
    //    权限分配返回所有page limit
    public List<RoleExtend> getRolesAuths(int page,int limit){
        Map<String,Integer> map=new HashMap();
//        设置查询开始时的的初始条数
        int start=(page-1)*limit;
        map.put("start",start);
        map.put("limit",limit);
        return roleMapper.selectAllAuths(map);
    }
//    自定义查看是否有对应的角色id和权限id
    public boolean getByRoleId(int role,int auth){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("auth_id",auth);
        map.put("role_id",role);
        return roleAuthMapper.getByRoleIdAuthId(map)!=null;

    }
//    返回所有的用户权限
    public List<Map<String,Object>> selectAllAuth(){

        return authMapper.selectAll();
    }
    @Transactional
    public boolean editRoleAuths(Integer role_id, Integer[] auths) {
        List<Map<String, Object>> extendAuths = authMapper.selectByRoleId(role_id);
        List<Integer> extendAuthList = new ArrayList();
        for (Map<String, Object> item : extendAuths) {
            extendAuthList.add((Integer) item.get("id"));
        }

        List<Integer> newAuthsList = Arrays.asList(auths);

        List<Integer> delList = new ArrayList();
        List<Integer> addList = new ArrayList();

        for (int i : extendAuthList) {
            if (!newAuthsList.contains(i)) {
                delList.add(i);
            }
        }

        for (int i : newAuthsList) {
            if (!extendAuthList.contains(i)) {
                addList.add(i);
            }
        }

        int res = 0;

        for (int i : addList) {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setAuthId(i);
            roleAuth.setRoleId(role_id);
            res += roleAuthMapper.insertSelective(roleAuth);
        }

        for (int i : delList) {
            Map<String, Object> map = new HashMap();
            map.put("role_id", role_id);
            map.put("auth_id", i);
            res += roleAuthMapper.deleteRoleAuth(map);
        }

        return res > 0;
    }











//    新增角色
    public int selectadd(String name){
        Role r=new Role();
        r.setRole(name);
        return roleMapper.insert(r);
    }
//    删除角色
    public int selectdel(int id){
        return roleMapper.deleteByPrimaryKey(id);
    }
//    修改角色
    public int selectedit(String name){
        Role r=new Role();
        r.setRole(name);
        return roleMapper.updateByPrimaryKey(r);
    }



}
